// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Player extends cc.Component {

    scrWidth : number;
    scrHeight : number;
    isSelected : boolean = false;

    @property(cc.Prefab)
    bulletPre : cc.Prefab = null;

    @property
    shootCD : number = 0.5;

    onLoad () {
        this.scrWidth = cc.view.getCanvasSize().width;
        this.scrHeight = cc.view.getCanvasSize().height;
    }

    start () {
        this.addTouchListener();
        this.schedule(() => {
            this.shoot();
        }, this.shootCD);
    }

    addTouchListener() {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, ()=> {
            this.isSelected = true;
        });

        this.node.on(cc.Node.EventType.TOUCH_END, ()=>{
            this.isSelected = false;
        });

        this.node.on(cc.Node.EventType.TOUCH_CANCEL, ()=>{
            this.isSelected = false;
        });

        this.node.on(cc.Node.EventType.TOUCH_MOVE, (e :cc.Event.EventTouch) => {
            if (this.isSelected) {
                this.node.setPosition(e.getLocation());
                if (this.node.x - this.node.width/2 < 0) {
                    this.node.x = this.node.width/2;
                } else if (this.node.x + this.node.width/2 > this.scrWidth) {
                    this.node.x = this.scrWidth - this.node.width/2;
                }

                if (this.node.y - this.node.height/2 < 0) {
                    this.node.y = this.node.height/2;
                } else if (this.node.y + this.node.height/2 > this.scrHeight) {
                    this.node.y = this.scrHeight - this.node.height/2;
                }
            }
        });
    }

    shoot() {
        cc.resources.load('audio/bullet', cc.AudioClip, (error, clip: cc.AudioClip) => {
            cc.audioEngine.playEffect(clip, false);
        });
        let bullet = cc.instantiate(this.bulletPre);
        bullet.x = this.node.x;
        bullet.y = this.node.y + this.node.height/2;
        bullet.setParent(cc.director.getScene());
    }
}
